package com.baseproject.widget;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.ColorRes;
import android.support.annotation.DimenRes;
import android.support.annotation.IdRes;
import android.support.annotation.NonNull;
import android.support.annotation.StyleRes;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;

import com.baseproject.R;


public abstract class SimpleDialog extends BaseDialog {

    private static final int SHOW_AUTO_WITH_HEIGHT = 1;
    private static final int SHOW_NORMAL_WITH_HEIGHT = 2;
    private static final int SHOW_FULL_WITH_HEIGHT = 3;
    private static final int SHOW_CUSTOM_WITH_HEIGHT = 4;
    private static final int SHOW_HALF_WITH = 5;
    private static final int SHOW_PERCENT_WITH = 6;
    private int showType;
    private float percent;
    protected Context context;
    protected View rootView;
    protected int screenWidth;
    protected int screenHeight;
    private WindowManager.LayoutParams dialogLp;
    private int dialogWidth;
    protected int customWidth;
    protected int customHeight;
    protected float radios;
    protected boolean isAutoWith;
    protected int dividerColor;
    protected int dividerWidth;

    public SimpleDialog(@NonNull Context context) {
        this(context, R.style.dialog_Dialog);
    }

    public SimpleDialog(@NonNull Context context, @StyleRes int themeResId) {
        super(context, themeResId);
        this.showType = 0;
        this.percent = 0.6F;
        this.isAutoWith = false;
        this.dividerColor = Color.parseColor("#D2D2D2");
        this.dividerWidth = 2;
        this.context = context;
        this.init();
    }

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setContentView(this.rootView);
        if (!this.isAutoWith) {
            this.dialogLp = this.getWindow().getAttributes();
            this.dialogLp.width = this.dialogWidth;
            this.getWindow().setAttributes(this.dialogLp);
        }

    }

    private void init() {
        this.dividerColor = this.context.getResources().getColor(R.color.dialog_divider);
        this.dividerWidth = this.context.getResources().getDimensionPixelSize(R.dimen.dialog_divider);
        DisplayMetrics dm = new DisplayMetrics();
        WindowManager wm = (WindowManager) this.context.getSystemService(Context.WINDOW_SERVICE);
        wm.getDefaultDisplay().getMetrics(dm);
        this.screenWidth = dm.widthPixels;
        this.screenHeight = dm.heightPixels;
        this.dialogWidth = (int) ((double) this.screenWidth * 0.8D);
        this.radios = this.context.getResources().getDimension(R.dimen.dialog_radios);
        this.rootView = LayoutInflater.from(this.context).inflate(this.getDialogLayoutId(), (ViewGroup) null);
        this.rootView.setBackgroundDrawable(this.getGradientDrawableRadius(this.context.getResources().getColor(R.color.dialog_background)));
        this.initView();
    }

    public void show() {
        if (!this.isShowing()) {
            super.show();
        }

        if (this.showType == 1) {
            this.setAutoWithHeight();
        } else if (this.showType == 2) {
            this.setNormalWithHeight();
        } else if (this.showType == 3) {
            this.setFullWithHeight();
        } else if (this.showType == 4) {
            this.setCustomWithHeight(this.customWidth, this.customHeight);
        } else if (this.showType == 5) {
            this.setHalfWith();
        } else if (this.showType == 6) {
            this.setPercentWith(this.percent);
        }

    }

    public void dismiss() {
        super.dismiss();
    }

    public void dismissDelayed() {
        this.dismissDelayed(1000L);
    }

    public void dismissDelayed(long delayMillis) {
        super.dismiss();
        (new Handler()).postDelayed(new Runnable() {
            public void run() {
                SimpleDialog.this.dismiss();
            }
        }, delayMillis);
    }

    public void setBackground(@ColorRes int resColor) {
        this.rootView.setBackgroundResource(resColor);
    }

    public void setBackgroundDrawable(@ColorRes int resColor) {
        this.rootView.setBackgroundDrawable(this.getGradientDrawableRadius(this.context.getResources().getColor(resColor)));
    }

    public void setBackgroundDrawable(Drawable drawable) {
        this.rootView.setBackgroundDrawable(drawable);
    }

    public void setDividerColor(@ColorRes int resColor) {
        this.dividerColor = this.context.getResources().getColor(resColor);
    }

    public void setDividerWidth(@DimenRes int res) {
        this.dividerWidth = this.context.getResources().getDimensionPixelSize(res);
    }

    public void setRadios(@DimenRes int res) {
        this.radios = (float) this.context.getResources().getDimensionPixelSize(res);
    }

    public void setAutoWithHeight() {
        this.showType = 1;
        this.getLayoutParams().width = -2;
        this.getLayoutParams().height = -2;
        this.getWindow().setAttributes(this.dialogLp);
    }

    public void setNormalWithHeight() {
        this.showType = 2;
        this.getLayoutParams().width = this.dialogWidth;
        this.getLayoutParams().height = -2;
        this.getWindow().setAttributes(this.dialogLp);
    }

    public void setPercentSixWith() {
        this.setPercentWith(0.6F);
    }

    @Override
    public void setContentView(@NonNull View view) {
        super.setContentView(view);
    }

    public void setPercentWith(float percent) {
        if ((double) percent < 0.1D) {
            percent = 0.1F;
        }

        if (percent > 1.0F) {
            percent = 1.0F;
        }

        this.showType = 6;
        this.getLayoutParams().width = (int) ((float) this.screenWidth * percent);
        this.getLayoutParams().height = -2;
        this.getWindow().setAttributes(this.dialogLp);
    }

    public void setHalfWith() {
        this.showType = 5;
        this.getLayoutParams().width = (int) ((double) this.screenWidth * 0.5D);
        this.getLayoutParams().height = -2;
        this.getWindow().setAttributes(this.dialogLp);
    }

    public void setFullWithHeight() {
        this.showType = 3;
        this.getLayoutParams().width = this.screenWidth;
        this.getLayoutParams().height = this.screenHeight;
        this.getWindow().setAttributes(this.dialogLp);
    }

    public void setCustomWithHeight(int width, int height) {
        this.showType = 4;
        this.customWidth = width;
        this.customHeight = height;
        this.getLayoutParams().width = width;
        this.getLayoutParams().height = height;
        this.setLayoutParams();
    }

    public WindowManager.LayoutParams getLayoutParams() {
        this.dialogLp = this.getWindow().getAttributes();
        return this.dialogLp;
    }

    public void setLayoutParams() {
        this.getWindow().setAttributes(this.dialogLp);
    }

    protected View getHorizontalLineView() {
        View view = new View(this.context);
        android.widget.LinearLayout.LayoutParams lp = new android.widget.LinearLayout.LayoutParams(this.dividerWidth, -1);
        view.setLayoutParams(lp);
        view.setBackgroundColor(this.dividerColor);
        return view;
    }

    protected View getVerticalLineView() {
        View view = new View(this.context);
        android.widget.LinearLayout.LayoutParams lp = new android.widget.LinearLayout.LayoutParams(-1, this.dividerWidth);
        view.setLayoutParams(lp);
        view.setBackgroundColor(this.dividerColor);
        return view;
    }

    protected GradientDrawable getGradientDrawableRadius(int color) {
        GradientDrawable gradientDrawable = new GradientDrawable();
        gradientDrawable.setShape(GradientDrawable.RECTANGLE);
        gradientDrawable.setCornerRadius(this.radios);
        gradientDrawable.setColor(color);
        return gradientDrawable;
    }

    protected GradientDrawable getGradientDrawable(int color) {
        GradientDrawable gradientDrawable = new GradientDrawable();
        gradientDrawable.setShape(GradientDrawable.RECTANGLE);
        gradientDrawable.setColor(color);
        return gradientDrawable;
    }

    protected GradientDrawable getGradientDrawableBottomLeft(int color) {
        GradientDrawable gradientDrawable = new GradientDrawable();
        gradientDrawable.setShape(GradientDrawable.RECTANGLE);
        gradientDrawable.setColor(color);
        gradientDrawable.setCornerRadii(new float[]{0.0F, 0.0F, 0.0F, 0.0F, 0.0F, 0.0F, this.radios, this.radios});
        return gradientDrawable;
    }

    protected GradientDrawable getGradientDrawableBottomRight(int color) {
        GradientDrawable gradientDrawable = new GradientDrawable();
        gradientDrawable.setShape(GradientDrawable.RECTANGLE);
        gradientDrawable.setColor(color);
        gradientDrawable.setCornerRadii(new float[]{0.0F, 0.0F, 0.0F, 0.0F, this.radios, this.radios, 0.0F, 0.0F});
        return gradientDrawable;
    }

    protected GradientDrawable getGradientDrawableBottomLeftRight(int color) {
        GradientDrawable gradientDrawable = new GradientDrawable();
        gradientDrawable.setShape(GradientDrawable.RECTANGLE);
        gradientDrawable.setColor(color);
        gradientDrawable.setCornerRadii(new float[]{0.0F, 0.0F, 0.0F, 0.0F, this.radios, this.radios, this.radios, this.radios});
        return gradientDrawable;
    }

    protected <T extends View> T getViewById(@IdRes int id) {
        return (T) this.rootView.findViewById(id);
    }

    protected abstract void initView();

    protected abstract int getDialogLayoutId();

    @Override
    protected boolean onTouchOutside(MotionEvent event) {
        return false;
    }
}
